package com.pshcp.patient.api;

import com.external.common.WebServiceException;
import com.external.pay.PayService;
import com.external.pay.domain.PayPackageDTO;
import com.external.service.WsRequestService;
import com.external.ws.soap.entity.dto.CasePrintDTO;
import com.external.ws.soap.entity.res.CaseMer;
import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.model.PatientMedicalRecordsPrintApply;
import com.pshcp.domain.model.PatientUser;
import com.pshcp.domain.vo.PatientMedicalRecordsPrintApplyVO;
import com.pshcp.patient.business.PatientMedicalPrintBusiness;
import com.pshcp.patient.domain.medicalprint.ao.MedicalApplyAO;
import com.pshcp.patient.domain.medicalprint.ao.MedicalApplyRecordAO;
import com.pshcp.patient.domain.medicalprint.ao.MedicalApplyToPayAO;
import com.pshcp.patient.domain.medicalprint.ao.OutMedicalListAO;
import com.pshcp.patient.domain.medicalprint.vo.MedicalPrintListVO;
import com.pshcp.patient.tool.MedicalPrintTools;
import com.pshcp.patient.tool.PatientMedicalPrintTools;
import com.pshcp.service.PatientMedicalRecordsPrintApplyService;
import com.pshcp.service.PatientService;
import com.pshcp.service.PatientUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Api(tags = {"患者端-病例复印"})
@Slf4j
@RestController
@RequestMapping("/api/patient/medicalPrint")
public class PatientMedicalPrintAPI {

    @Autowired
    private WsRequestService wsRequestService;

    @Autowired
    private PatientMedicalRecordsPrintApplyService printApplyService;

    @Autowired
    private PatientMedicalPrintBusiness medicalPrintBusiness;

    @Autowired
    private PatientService patientService;

    @Autowired
    private PayService payService;

    @Autowired
    private PatientUserService patientUserService;

    /**
     * 使用测试数据
     */
    @Value("${ws.test:false}")
    private Boolean hisTest;

    @ApiOperation("门诊/住院-病例列表")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "门诊/住院-病例列表")
    @PostMapping("/medicalList")
    public ResultDTO<List<MedicalPrintListVO>> outMedicalList(@RequestBody OutMedicalListAO listAO) throws WebServiceException {
        Patient patient = patientService.findById(listAO.getPatientId());
        CasePrintDTO casePrintDTO = PatientMedicalPrintTools.buildWsDTO(patient, listAO);
        List<CaseMer> caseMers = wsRequestService.casePrint(casePrintDTO);
        List<MedicalPrintListVO> voList = CommonTool.copyPropertiesList(caseMers, MedicalPrintListVO.class);
        if(hisTest){
            voList = PatientMedicalPrintTools.buildTestData();
        }
        return ResultBuilder.success(voList);
    }

    @ApiOperation("病例申请提交")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.ADD, desc = "病例申请提交")
    @PostMapping("/medicalApply")
    public ResultDTO medicalApply(@RequestBody MedicalApplyAO medicalApplyAO) throws BizException {
        Patient patient = patientService.findById(medicalApplyAO.getPatientId());
        CommonTool.checkObj(patient, "10120002");
        PatientMedicalRecordsPrintApply printApply = MedicalPrintTools.buildPrintApplyAO2Model(medicalApplyAO, patient);
        printApplyService.insert(printApply);
        return ResultBuilder.success(printApply.getId());
    }

    @ApiOperation("病例申请记录")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "病例申请记录")
    @PostMapping("/medicalApplyRecord")
    public ResultDTO<PageList<PatientMedicalRecordsPrintApplyVO>> medicalApplyRecord(@RequestBody MedicalApplyRecordAO applyRecordAO) {
        PageList<PatientMedicalRecordsPrintApply> pageList = printApplyService.list(MedicalPrintTools.buildApplyRecordAO2DTO(applyRecordAO));
        PageList<PatientMedicalRecordsPrintApplyVO> voPageList = CommonTool.buildPageList(pageList, PatientMedicalRecordsPrintApplyVO.class);
        return ResultBuilder.success(voPageList);
    }

    @ApiOperation("病例打印去支付")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.ADD, desc = "病例打印去支付")
    @PostMapping("/toPay")
    public ResultDTO<PayPackageDTO> toPay(@RequestBody MedicalApplyToPayAO toPayAO) throws BizException {
        PatientMedicalRecordsPrintApply printApply = printApplyService.findById(toPayAO.getId());
        CommonTool.checkObj(printApply, "10230001");
        //状态 0待审核 1审核通过
        if("0".equals(printApply.getBizStatus())){
            return ResultBuilder.failure("10230002");
        }
        if(!RegPayStatusEnum.WAIT_PAY.getCode().equals(printApply.getPayStatus())){
            return ResultBuilder.failure("10230002");
        }
        if(BigDecimal.ZERO.compareTo(printApply.getAmount()) >= 0){
            medicalPrintBusiness.enterOrder(printApply);
            PayPackageDTO payPackageDTO = new PayPackageDTO();
            payPackageDTO.setSkipPay(true);
            return ResultBuilder.success(payPackageDTO);
        }
        PatientUser patientUser = patientUserService.findById(ReqComHold.getReqCom().getUserId());
        toPayAO.setOpenid(patientUser.getOpenid());

        PayPackageDTO payPackageDTO = payService.callPay(MedicalPrintTools.buildPayDTO(printApply,toPayAO));
        return ResultBuilder.success(payPackageDTO);
    }

}
